#ifndef _CLI_H
#define _CLI_H

#include <vector>
#include <string>
#include <termios.h>
#include <pthread.h>
#include <time.h>
#include "syscore.h"
#include "apis.h"
#include "common.h"

using namespace std;

namespace cli
{
   const U16 MAX_BUFF_SIZE = 4096;
   // TODO set Module ID and DEV KEY ID for CLI message
   const U16 MAX_ERROR_CODE = 25;
   const U16 MAX_CMD_LENGTH = 256;

   typedef struct _value_string {
       U32          value;
       const char   *strptr;
   } value_string;

   static const value_string operationResult[] = {
       { PARA_VALUE_RESULT_SUCCESS, "Successed"},
       { PARA_VALUE_RESULT_DOING, "Operation is ongoing"},
       { PARA_VALUE_RESULT_FAILED, "Failed"},
       { 0xffffffff, NULL}
   };

   const value_string errorInfo[] = {
       { PARA_VALUE_ERROR_NO_ERROR,  "No Error"},
       { PARA_VALUE_ERROR_ILLEGAL_SRC, "Illigal Source"},
       { PARA_VALUE_ERROR_DST_MODULE_INACTIVE, "Destination Module Inactive"},
       { PARA_VALUE_ERROR_DST_MODULE_BUSY, "Destination Module Busy"},
       { PARA_VALUE_ERROR_NO_MEMORY, "No Memory"},
       { PARA_VALUE_ERROR_UNKNOWN_CMDTYPE, "Unknow Comand Type"},
       { PARA_VALUE_ERROR_UNKNOWN_CMDCODE, "Unknow Comand Code"},
       { PARA_VALUE_ERROR_INVALID_CMDLENGTH, "Invalid Comand Length"},
       { PARA_VALUE_ERROR_UNKNOWN_PARATYPE, "Unknow Parameter Type"},
       { PARA_VALUE_ERROR_INVALID_PARALENGTH, "Unknow Parameter Length"},
       { PARA_VALUE_ERROR_INVALID_PARAVALUE, "Invalid Parameter Value"},
       { PARA_VALUE_ERROR_ILLEGAL_PARA_STRUCT, "Illigal Parameter Struct"},
       { PARA_VALUE_ERROR_NOT_ENOUGH_PARA, "No Enough Parameter"},
       { PARA_VALUE_ERROR_CANNOT_GET_ID, "Can not Get ID"},
       { PARA_VALUE_ERROR_OPEN_DATABASE_FAILED, "Open Database failed"},
       { PARA_VALUE_ERROR_INSERT_RECORD_FAILED, "Insert Record failed"},
       { PARA_VALUE_ERROR_DELETE_RECORD_FAILED, "Delete Record failed"},
       { PARA_VALUE_ERROR_END_NOT_GREATER_THAN_BEGIN, "Start Smaller Than End"},
       { PARA_VALUE_ERROR_NUM_OUT_OF_RANGE, "Out of Rang"},
       { PARA_VALUE_ERROR_DATAFLOW_BUSY, "Data Flow Busy"},
       { PARA_VALUE_ERROR_NOT_DATAFLOW_PEER, "No Data Flow Peer"},
       { PARA_VALUE_ERROR_NO_DB, "NO DB"},
       { PARA_VALUE_ERROR_GSMBASEBAND_INACTIVE, "GSM Baseband is Inactive"},
       { PARA_VALUE_ERROR_INVALID_PARAMETERS, "Invalid Parameters"},
       { PARA_VALUE_ERROR_PA_OK, "PA OK"},
       { PARA_VALUE_ERROR_PA_NOK, "PA Not OK"},
       { PARA_VALUE_ERROR_PA_NOT_FOUND, "PA Not Found"},
       { PARA_VALUE_ERROR_PA_OFF, "PA OFF"},
       { PARA_VALUE_ERROR_PA_NOT_SUPPORTED, "PA Not Supported"},
       { PARA_VALUE_ERROR_PA_UART_READ_ZERO, "UART Read Zero"},
       { PARA_VALUE_ERROR_PA_UART_READ, "UART Read Error"},
       { PARA_VALUE_ERROR_PA_UART_WRITE, "UART Write Error"},
       { PARA_VALUE_ERROR_PA_MAGIC, "PA Magic Error"},
       { PARA_VALUE_ERROR_PA_CRC, "CRC Error"},
       { PARA_VALUE_ERROR_PA_MOD_ADDR, "Mod Addr Error"},
       { PARA_VALUE_ERROR_PA_COM_CODE, "Com Code Error"},
       { PARA_VALUE_ERROR_PA_RSP_FLAG, "Response Flag Error"},
       { PARA_VALUE_ERROR_PA_PARAM, "Wrong PA Param"},
       { PARA_VALUE_ERROR_FW_OK, "OK"},
       { PARA_VALUE_ERROR_FW_NOK, "Not OK"},
       { PARA_VALUE_ERROR_FW_NOT_FOUND, "Not Found"},
       { PARA_VALUE_ERROR_FW_OFF, "FW OFF"},
       { PARA_VALUE_ERROR_FW_NOT_SUPPORTED, "Not Supported"},
       { PARA_VALUE_ERROR_FW_ERR_PARAM, "ERROR Param"},
       { PARA_VALUE_ERROR_FW_ERR_MD5SUM, "ERROR MD5SUM"},
       { PARA_VALUE_ERROR_FW_ERR_DOWNLOAD, "Download Error"},
       { PARA_VALUE_ERROR_WCDMA_DAC_FAILED, "DAC failed"},
       { PARA_VALUE_ERROR_WCDMA_CONFIG_FAILED, "Config failed"},
       { PARA_VALUE_ERROR_WCDMA_TXSTOP_FAILED, "Radio output stop failed"},
       { PARA_VALUE_ERROR_WCDMA_TXSTART_FAILED, "CRC output start failed"},
       { PARA_VALUE_ERROR_WCDMA_AUTOLAC_FAILED, "Auto LAC failed"},
       { PARA_VALUE_ERROR_WCDMA_POWER_FAILED, "Power setting failed"},
       { PARA_VALUE_ERROR_WCDMA_RESTART_FAILED, "Restart failed"},
       { PARA_VALUE_ERROR_WCDMA_TIME_FAILED, "Time setting failed"},
       { PARA_VALUE_ERROR_WCDMA_IP_FAILED, "IP setting failed"},
       { PARA_VALUE_ERROR_WCDMA_PILOT_FAILED, "Pilot setting failed"},       
       { 0xffffffff, NULL}
    };   

    const value_string paState[] = {
        { PARA_VALUE_PA_OFF, "PA OFF"},
        { PARA_VALUE_PA_ON, "PA ON"},
        { 0xff, "Wrong State"}
    };   
    
    const value_string simState[] = {
        { 0, "Unknown State"},
        { 1, "Inserted"},
        { 255, "Not Inserted"}
    };

    const value_string smsState[] = {
        { PARA_VALUE_3G_SMS_OFF, "SMS OFF"},
        { PARA_VALUE_3G_SMS_ON, "SMS ON"},
        { 255, "Unknow State"}
    };

    const value_string wirelessState[] = {
        { PARA_VALUE_GSM_WILELESS_OFF, "GSM OFF"},
        { PARA_VALUE_GSM_WILELESS_ON, "GSM ON"},
        { 255, "Unknow State"}
    };

    const value_string snifferState[] = {
        { PARA_VALUE_GSM_SNIFFER_OFF, "SNIFFER OFF"},
        { PARA_VALUE_GSM_SNIFFER_ON, "SNIFFER ON"},
        { 255, "Unknow State"}
    };    

    const value_string gpsState[] = {
        { 0, "Offline"},
        { 1, "Online"},
        { 255, "Unknow State"}
    };  

    const value_string eutranSnifferState[] = {
        { PARA_VALUE_EUTRAN_SNIFFER_STATUS_IDLE, "SNIFFER idle"},
        { PARA_VALUE_EUTRAN_SNIFFER_STATUS_ONGOING, "SNIFFER ongoing"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_UNAVAIL, "BBU unavailable"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_IDLE, "BBU idle"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_RSSI_MEASING, "BBU RSSI measuring"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_CELL_SEARCH, "BBU cell searching"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_MIB_READING, "MIB reading"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_SBI1_READING, "SIB1 reading"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_SBIX_READING, "SIBX reading"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFER_STATUS_FINISH, "SNIFFER finish"},
        { 255, "Unknow State"}
    };

    const value_string eutranBbuState[] = {
        { PARA_VALUE_EUTRAN_BBU_INVALID, "BBU invalid"},
        { PARA_VALUE_EUTRAN_BBU_INITIALIZING, "BBU is initializing"},
        { PARA_VALUE_EUTRAN_BBU_INIT_TO_CTRL_BOARD, "BBU init to CTRL board"},
        { PARA_VALUE_EUTRAN_BBU_INIT_CONFIGING, "BBU init config"},    
        { PARA_VALUE_EUTRAN_BBU_AVAILABLE, "BBU available"},
        { PARA_VALUE_EUTRAN_BBU_SNIFFERING, "BBU sniffering"},
        { PARA_VALUE_EUTRAN_BBU_CELL_CONFIGING, "BBU cell configing"},
        { PARA_VALUE_EUTRAN_BBU_LOCATION, "BBU location"},
        { 255, "Unknow State"}
    };

    class MsgHeader
    {
        public:
            U16 destId_m;
            U16 srcId_m;
            U16 msgType_m;
            U16 msgLength_m;
    };
    
    class CliMessage
    {
        public:
            MSGHEADER msgHeader_m;
            CMDHEADER cmdHeader_m;
            char cmdPayload_m[MAX_MSG_SIZE];
    };


    class Command
    {
        public:
            Command(U16 dst_id, U16 type, U16 code, string name, string tips): 
                dst_id_m(dst_id),type_m(type),code_m(code),name_m(name),tips_m(tips){};
            U16 getDstID(void){return dst_id_m;}
            U16 getType(void){return type_m;}
            U16 getCode(void){return code_m;}
            const string & getName(void){return name_m;}
            const string & getTips(void){return tips_m;}
        private:
            U16 dst_id_m;
            U16 type_m;
            U16 code_m;
            string name_m;
            string tips_m;
    };

    class Module
    {
        public:
            typedef struct{
                 double longitude;
                 double latitude;
                 double altitude;
            } GPSPOSITION;        
		   
			typedef struct{
				U8 ID;
	            U8 state;
                S8 temp;
 	            U8 upatt;
	            U8 dnatt;
	            S8 dn_power;
	            S16 dn_10_power;
	            U8 sw10ratio;				
             }PASTATUS;

        public:
            void createCmd(U16 dst_id, U16 type, U16 code, string name, string tips);
            const string &getName(void){return name_m;}
            void listCmds(void);
            void visit(void);
            void handleCmd(const char *cmd);
            void handleCmd(vector<string> &cmdParams);
            bool matchCmd(char *cmd);
            bool frontMatchCmd(char *cmd);
            U16 compensateCmd(char *cmd);
            U16 getModuleId(){ return moduleId_m;}
            Command * extractCmd(string orgStr);
            const char* matchStringValue(const value_string *vs, const U32 val);

            U32 paramPackParam(PPARAM *ppParam, U32 paraType, U32 paraLength, U8** pParaValue);
            U32 packMsg1Param(U32 paraType, U32 paraLength, U8 *paraValue, char *payLoad);
            U32 packMsg2Param( U32 paraType1,
                               U32 paraLength1,
                               U8 *paraValue1,
                               U32 paraType2,
                               U32 paraLength2,
                               U8 *paraValue2,
                               char *payLoad
                               );
            U32 packMsg3Param(
                               U32 paraType1,
                               U32 paraLength1,
                               U8 *paraValue1,
                               U32 paraType2,
                               U32 paraLength2,
                               U8 *paraValue2,
                               U32 paraType3,
                               U32 paraLength3,
                               U8 *paraValue3,
                               char *payLoad
                               );
            bool decodeGeneralCmdResponse(PCMDHEADER pCmd, string & out_r);

            string& trim(std::string &s)
            {
               if (s.empty()) 
               {
                  return s;
               }

               s.erase(0,s.find_first_not_of(" "));
               s.erase(s.find_last_not_of(" ") + 1);
               return s;
            };

        protected:
            Module(U16 moduleId, string name):moduleId_m(moduleId), name_m(name), maxCmdWidth_m(5)
            {
                createCmd(moduleId, 0, 0,           "help", "List all supported commands.");
                createCmd(moduleId, 0, 0,           "exit", "Back to module selection menu.");
                cmdHistoryIdx_m = 0;
            }
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len) = 0;
            virtual bool decode(PCMDHEADER pCmd, string & out_r) = 0;            

            U16 moduleId_m;
            string name_m;
            U16 maxCmdWidth_m;
            vector<Command *> cmdList_m;
            
            struct termios term_m;
            vector<string *> cmdHistory_m;
            U16 cmdHistoryIdx_m;
    };

    class ModuleGsm : public Module
    {
        typedef struct
        {
            U32   frequency;
            U32   mcc;
            U32   mnc;
            U32   lac;
            U32   band;
            U32   carrier;
            U32   cro; 
            S32   rssiMin;
            U32   thresholdDelta;  
            U32   captime;
            U32   dnatt;
            U32   upatt;
            U32   cnum;
            U32   configMode;
            U32   workMode; 
            U32   startFreq900;
            U32   endFreq900;
            U32   startFreq1800;
            U32   endFreq1800;
            U32   freqOffset;
        }geranParam_t;

        typedef struct
        {
            U8 autoBccMode;
            U8 isAuto;
            U8 hour;
            U8 minutes;
            U8 second;
            U8 snifferInterval;
            U8 reverved[2];
        }snifferParam_t;

        typedef struct cellItemTag
        {
           U16	wARFCN;
           U16	wBSIC;
           S8	cRSSI;
           U8	bReserved;
           S8	cC1;
           U8	bC2;
        }cellItem;
		
		typedef struct rawSnifferResultTag
		{
			U32 		 bGlobalCellId;
			U8			 bPLMNId[5];
			S8			 cRSSI; 
			U16 		 wLAC;
			U16 		 wBSIC;
			U16 		 wARFCN;
			S8			 cC1;
			U8			 bNbCellNum;
			U8			 bReserved;
			U8			 bC2;
			cellItem	 stNbCell[32];
		}rawSnifferResult;

        typedef struct statTag
        {
            U16         wARFCN;
            S8          cRSSI;
            U8          reserved;
            U32         count;
            bool operator >(const statTag& rhs) const  
            {    
                return count > rhs.count;    
            }  
            bool operator <(const statTag& rhs) const  
            {    
                return count < rhs.count;    
            }     
        }statArfcn;

        public:
            ModuleGsm();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
           // bool comp(const statArfcn &stat1, const statArfcn &stat2);
            bool decodeGetGsmMOduleStatus(PCMDHEADER pCmd, string & out_r);
            bool decodeGetGsmModuleConfiguration(PCMDHEADER pCmd, string & out_r);
            bool buildSetCmdMsg(vector<string> &params, char *payload, U16 &len);
            bool buildGetModuleCfg(vector<string> &params, char *payload, U16 &len);
            bool buildCfgGsmCapModule(vector<string> &params, char *payload, U16 &len);
            bool buildCfgGsmSnifferModule(vector<string> &params, char *payload, U16 &len);
            bool buildCfgRecuringInterval(vector<string> &params, char *payload, U16 &len);
            bool buildSetGsmTxAttuation(vector<string> &params, char *payload, U16 &len);
            bool decodeGetGsmSendingCounter(PCMDHEADER pCmd, string & out_r);
            bool decodeGetSnifferResult(PCMDHEADER pCmd, string & out_r);
            bool decodeGetSnifferConfiguration(PCMDHEADER pCmd, string & out_r);
            bool decodeGetVerison(PCMDHEADER pCmd, string & out_r);
            bool decodeGetGsmChoiceArfcn(PCMDHEADER pCmd, string & out_r);
        private:
            bool hasMore;
            U32  recordNum;
            //thread id
            pthread_t tid;
            //thread status
            int threadStatus;
            
            vector<statArfcn>   stat;
            int          statNumber;
            //
            static void* rcvSnifferResultflow(void* pVoid);
            bool createRcvSnifferResultThread();
            void handleSnifferResultMessage();			
    };

    class ModuleCdma : public Module
    {
        public:
            ModuleCdma();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool decodeGetCdmaMOduleStatus(PCMDHEADER pCmd, string & out_r);
            bool decodeGetCdmaModuleConfiguration(PCMDHEADER pCmd, string & out_r);
            bool buildSetCdmaTxAttuation(vector<string> &params, char *payload, U16 &len);
            bool decodeGetCdmaModuleStatus(PCMDHEADER pCmd, string & out_r);
    };
    	
    class ModuleWcdma : public Module
    {
		typedef struct
		{
			U32    mcc;
			U32    mnc;
			U32    lac;
			U32    arfcn;	
			U32    psc; 	 //
			U32    rac;
			U32    cellid;
			U32    autolac;
			U32    dac; 
			U32    txPower;
			U32    rxPower;
		}wcdmaParam_t;
                
        public:
            ModuleWcdma();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool decodeGetWcdmaModuleStatus(PCMDHEADER pCmd, string & out_r);
            bool decodeGetWcdmaModuleConfiguration(PCMDHEADER pCmd, string & out_r);
            bool buildGetModuleCfg(vector<string> &params, char *payload, U16 &len);
            bool buildCfgWcdmaCapModule(vector<string> &params, char *payload, U16 &len);
            bool buildSetWcdmaTxAttuation(vector<string> &params, char *payload, U16 &len);
            bool decodeGetWcdmaSendingCounter(PCMDHEADER pCmd, string & out_r);
    };    

    class ModuleTdscdma : public Module
    {
		typedef struct
		{
			U32    mcc;
			U32    mnc;
			U32    lac;
			U32    arfcn;	
			U32    psc; 	 //
			U32    rac;
			U32    cellid;
			U32    autolac;
			U32    dac; 
			U32    txPower;
			U32    rxPower;
		}tdscdmaParam_t;
                
        public:
            ModuleTdscdma();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool decodeGetTdscdmaModuleStatus(PCMDHEADER pCmd, string & out_r);
            bool decodeGetTscdmaModuleConfiguration(PCMDHEADER pCmd, string & out_r);
            bool buildGetModuleCfg(vector<string> &params, char *payload, U16 &len);
            bool buildCfgTdscdmaCapModule(vector<string> &params, char *payload, U16 &len);
            bool buildSetTdscdmaTxAttuation(vector<string> &params, char *payload, U16 &len);
            bool decodeGetTdscdmaSendingCounter(PCMDHEADER pCmd, string & out_r);
            bool buildGetTdscdmaModuleInfo(vector<string> &params, char *payload, U16 &len);
            bool decodeGetTdscdmaModuleInfo(PCMDHEADER pCmd, string & out_r);
            bool decodeGetTdscdmaSnifferResult(PCMDHEADER pCmd, string & out_r);
            bool decodeGetTscdmaModuleStatus(PCMDHEADER pCmd, string & out_r);
    };    
	
    class Module3G : public Module
    {
        public:
            Module3G();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool decodeGetSimAndSmsState(PCMDHEADER pCmd, string & out_r);
            bool buildSmsReport(vector<string> &params, char *payload, U16 &len);
            bool buildSendSms(vector<string> &params, char *payload, U16 &len);
            bool sendModuleTest(vector<string>& params, char* payload, U16& len); 
    };
    
	class ModulePa : public Module
    {
        public:
            ModulePa();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);

            bool buildSetPaState(vector<string> &params, char *payload, U16 &len);
            bool buildGetPaStatus(vector<string> &params, char *payload, U16 &len);
            bool buildGetPaSoft_Ware_Version(vector<string> &params, char *payload, U16 &len);
	     	bool decodeGeneralPaInfoMsg(PCMDHEADER pCmd, string & out_r);
	     	bool buildSetPaUpatt(vector<string> &params, char *payload, U16 &len);
            bool buildSetPaDnatt(vector<string> &params, char *payload, U16 &len);
			bool buildSetPaTempthreshold(vector<string> &params, char *payload, U16 &len);
			bool buildSetPaPowerthreshold(vector<string> &params, char *payload, U16 &len);
	     	bool decodePaSoft_Ware_VersionMsg(PCMDHEADER pCmd, string & out_r);
    };

    class ModuleData : public Module
    {
        typedef struct{
            U32 totallen;
            U64 uptime;
            int slotId;
            U8  *imsi;
            U8  *imei;
            U8  *tmsi;
        } phoneInfo_t;

        public:
            ModuleData();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool buildSetBWListCfg(vector<string> &params, char *payload, U16 &len);
            bool buildStartShowPhoneInfo(vector<string> &params, char *payload, U16 &len);
            bool buildCleanData(char *payload, U16 &len);
            bool decodePhoneInfoData(PCMDHEADER pCmd, string & out_r);
            bool decodePhoneInfoCmdResponse(PCMDHEADER pCmd, string & out_r);
            void sendPhoneInfoAck(U16 cmdType, U16 cmdCode);

        private:
            bool hasMore;
            U32  recordNum;
            //thread id
            pthread_t tid;
            //thread status
            int threadStatus;
            //
            static void* rcvPhoneInfoDataflow(void* pVoid);
            bool createRcvPhoneInfoThread();
            void handlePhoneInfoMessage();
    };

    class ModuleSystem : public Module
    {
        public:
            ModuleSystem();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool decodeSystemInformation(PCMDHEADER pCmd, string & out_r);
            bool setSystemTime(vector<string> &params, char *payload, U16 &len);
            bool setSystemLogLevel(vector<string> &params, char *payload, U16 &len);
            bool setSystemDebugLevel(vector<string> &params, char *payload, U16 &len);
            bool setSystemRemoteSsh(vector<string> &params, char *payload, U16 &len);
    };

    class ModulePower : public Module
    {
        public:
            ModulePower();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);

            bool encodePowerMgrShutdownModule(vector<string> &params, char *payload, U16 &len);
            bool encodePowerMgrRestartModule(vector<string> &params, char *payload, U16 &len);
            bool encodePowerMgrPoweronModule(vector<string> &params, char *payload, U16 &len);
    }; 

    class ModuleGps : public Module
    {
        public:
            ModuleGps();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool decodeGpsStatus(PCMDHEADER pCmd, string & out_r);
    };     


    class ModuleMessage : public Module
    {
        public:
            ModuleMessage();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
    };     
    
    
    class ModuleFwmgr : public Module
    {
        public:
            ModuleFwmgr();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);

            bool sendFwmgrUpgradeCommand(vector<string> &params, char *payload, U16 &len);
            bool sendFwmgrInstallCommand(vector<string> &params, char *payload, U16 &len);
    };

    class ModuleWifi : public Module
    {
        public:
            ModuleWifi();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);

            bool decodeGetWifiModuleStatus(PCMDHEADER pCmd, string & out_r);
    };

    class ModuleScanner : public Module
    {
        public:
            ModuleScanner();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);

            bool decodeGetSnifferResult(PCMDHEADER pCmd, string & out_r);
    };

    class ModuleFep : public Module
    {
        public:
            ModuleFep();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
            bool sendFepUpgradeCommand(vector<string> &params, char *payload, U16 &len);
            bool set_Fep_loglevel(vector<string> &params, char *payload, U16 &len);
            bool sendModuleTest(vector<string>& params, char* payload, U16& len); 
    };

    class ModuleCollection
    {
        public:
            static ModuleCollection * getInstance(void)
            {
                if(instance_m==NULL)
                {
                    instance_m = new ModuleCollection();
                }
                return instance_m;
            }
            
            void addModule(Module *module){moduleList_m.push_back(module);}
            void listModules(void);
            void processCmd(int argc, char **argv);
            void queryModuleState(void);
            
        private:
            vector<Module *> moduleList_m;
            static ModuleCollection * instance_m;
    };

    class ModuleEutran : public Module
    {
        public:
            ModuleEutran();
            virtual bool encode(U16 cmdCode, vector<string> &params, char *payload, U16 &len);
            virtual bool decode(PCMDHEADER pCmd, string & out_r);
    
        private:
            bool sendRtParaCfg(vector<string>& params, char* payload, U16& len);
            bool sendUeIdFilterSiwtch(vector<string>& params, char* payload, U16& len);
            bool sendTxPowerStdDbGet(vector<string>& params, char* payload, U16& len);
            bool sendStdTxPowerGet(vector<string>& params, char* payload, U16& len);
            bool sendStdTxPower(vector<string>& params, char* payload, U16& len);
            bool sendUeRedirect(vector<string>& params, char* payload, U16& len);
            bool sendGpsStateReq(vector<string>& params, char* payload, U16& len);
            bool sendShowBbuInfo(vector<string>& params, char* payload, U16& len);
            bool sendBbuUpgrade(vector<string>& params, char* payload, U16& len);
            bool sendSyncModeGet(vector<string>& params, char* payload, U16& len);
            bool sendSyncModeSetting(vector<string>& params, char* payload, U16& len);
            bool sendBbuRebootReq(vector<string>& params, char* payload, U16& len);
            bool sendBbuStateReq(vector<string>& params, char* payload, U16& len);
            bool sendSoftwareVerReq(vector<string>& params, char* payload, U16& len);
            bool sendTxPowerCfg(vector<string>& params, char* payload, U16& len);
            bool sendInitCfgCmd(vector<string>& params, char* payload, U16& len);
            bool sendSetBbuIpCmd(vector<string>& params, char* payload, U16& len);
            bool sendSnifferStartCmd(vector<string>& params, char* payload, U16& len);
            bool sendSnifferStopCmd(vector<string>& params, char* payload, U16& len);
            bool sendGetSnifferStatusCmd(vector<string>& params, char* payload, U16& len);
            bool sendSnifferResetCmd(vector<string>& params, char* payload, U16& len);
            bool sendRssiConfigCmd(vector<string>& params, char* payload, U16& len);
            bool sendGetSnifferResultCmd(vector<string>& params, char* payload, U16& len);
            bool sendRssiThresholdCmd(vector<string>& params, char* payload, U16& len);
            bool sendCellConfigCmd(vector<string>& params, char* payload, U16& len);
            bool sendCellUpdateCmd(vector<string>& params, char* payload, U16& len);
            bool sendLocModeSettingCmd(vector<string>& params, char* payload, U16& len);
            bool sendAutoSnfCfgCmd(vector<string>& params, char* payload, U16& len);  
            bool sendModuleTest(vector<string>& params, char* payload, U16& len);          
            bool decodeStdTxPowerDbGetCmd(PCMDHEADER pCmd, string & out_r);
            bool decodeStdTxPowerGetCmd(PCMDHEADER pCmd, string & out_r);
            bool decodeAutoSnfCfgCmd(PCMDHEADER pCmd, string & out_r);
            bool decodeShowBbuInfo(PCMDHEADER pCmd, string & out_r);
            bool decodeBbuStateReq(PCMDHEADER pCmd, string & out_r);
            bool decodeSnifferStatus(PCMDHEADER pCmd, string & out_r);
            bool decodeSnifferResult(PCMDHEADER pCmd, string & out_r);
            bool decodeRssiThreshold(PCMDHEADER pCmd, string & out_r);
            bool decodeCellUpdateRsp(PCMDHEADER pCmd, string & out_r);
            bool decodeSoftwareVersion(PCMDHEADER pCmd, string & out_r);
            bool decodeSyncState(PCMDHEADER pCmd, string & out_r);
            bool decodeGpsState(PCMDHEADER pCmd, string & out_r);
            bool convertToBandList(const std::string& params, std::vector<uint8_t>& bandList);
            bool convertToEarfcnList(const std::string& para, std::vector<std::pair<uint16_t, uint16_t> >& arfcnRangeList);
            bool convertToPciList(const std::string& para, std::vector<uint16_t>& pciList);
            bool convertToU16TypeList(const std::string& para, std::vector<uint16_t>& list);
            bool checkTimeFormat(const std::string& para);
    };


    class MessageHandler
    {
        public:
            MessageHandler():txQueueId_m(0), rxQueueId_m(0){}
            static MessageHandler * getInstance(void)
            {
                if(instance_m==NULL)
                {
                    instance_m = new MessageHandler();
                }
                return instance_m;
            }
           
           void init(void);
           bool sendMsg(char *msg, U16 length);
           bool recvMsg(char *msg);
           bool recvMsgNoWait(char *msg);
           bool cleanQueue();
           
        private:
            static MessageHandler * instance_m;
            int txQueueId_m;
            int rxQueueId_m;
            
    };

}

#define SA struct sockaddr
#endif


